home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Games / SoundSprocketTest / TS3Utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  11.2 KB  |  470 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    File:        TS3Utils.c
  3.  *
  4.  *    Contents:    Some utilities.
  5.  *
  6.  *    Copyright © 1996 Apple Computer, Inc.
  7.  */
  8.  
  9. #include <assert.h>
  10. #include <math.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #include "TS3Utils.h"
  15.  
  16.  
  17. #ifndef HONOR_RANGE
  18.     #define HONOR_RANGE        0
  19. #endif
  20.  
  21.  
  22. static UserItemUPP            gUtilsOKUserItemProc    = NULL;
  23.  
  24.  
  25. static pascal void Utils_OKUserItem(
  26.     DialogPtr            inDialog,
  27.     short                inItem);
  28.  
  29.  
  30. /* =============================================================================
  31.  *        Utils_Init (external)
  32.  *
  33.  *    Initializes the utilities.
  34.  * ========================================================================== */
  35. void Utils_Init(
  36.     void)
  37. {
  38. }
  39.  
  40.  
  41. /* =============================================================================
  42.  *        Utils_Exit (external)
  43.  *
  44.  *    Prepares for exit.
  45.  * ========================================================================== */
  46. void Utils_Exit(
  47.     void)
  48. {
  49.     if (gUtilsOKUserItemProc != NULL)
  50.     {
  51.         DisposeRoutineDescriptor(gUtilsOKUserItemProc);
  52.         gUtilsOKUserItemProc = NULL;
  53.     }
  54. }
  55.  
  56.  
  57. /* =============================================================================
  58.  *        Utils_Interval (external)
  59.  *
  60.  *    Returns the interval, in seconds, between two results of Microseconds.
  61.  * ========================================================================== */
  62. float Utils_Interval(
  63.     const UnsignedWide*    inPrevTime,
  64.     const UnsignedWide*    inCurrTime)
  65. {
  66.     //• Should actually take high words into consideration too
  67.     return 0.000001*(inCurrTime->lo - inPrevTime->lo);
  68. }
  69.  
  70.  
  71. /* =============================================================================
  72.  *        Utils_OKUserItem (internal)
  73.  *
  74.  *    Draws the user item used for framing the OK button.  The user item should
  75.  *    be four pixels larger than the OK button in each direction.
  76.  * ========================================================================== */
  77. pascal void Utils_OKUserItem(
  78.     DialogPtr            inDialog,
  79.     short                inItem)
  80. {
  81.     short                itemType;
  82.     Handle                itemHandle;
  83.     Rect                itemBounds;
  84.     
  85.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  86.     
  87.     PenSize(3, 3);
  88.     FrameRoundRect(&itemBounds, 16, 16);
  89.     PenSize(1, 1);
  90. }
  91.  
  92.  
  93. /* =============================================================================
  94.  *        Utils_GetOKUserItemProc (external)
  95.  *
  96.  *    Returns the UPP for the dialog user item that outlines the default button.
  97.  * ========================================================================== */
  98. UserItemUPP Utils_GetOKUserItemProc(
  99.     void)
  100. {
  101.     if (gUtilsOKUserItemProc == NULL)
  102.     {
  103.         gUtilsOKUserItemProc = NewUserItemProc(Utils_OKUserItem);
  104.         assert(gUtilsOKUserItemProc != NULL);
  105.     }
  106.     
  107.     return gUtilsOKUserItemProc;
  108. }
  109.  
  110.  
  111. /* =============================================================================
  112.  *        Utils_SetStr255Field (external)
  113.  *
  114.  *    Changes the text of the given dialog item to the given string value.  If
  115.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  116.  *    of course be editable or static text.
  117.  * ========================================================================== */
  118. void Utils_SetStr255Field(
  119.     DialogPtr            inDialog,
  120.     short                inItem,
  121.     ConstStr255Param    inValue,
  122.     Boolean                inHasValue)
  123. {
  124.     short                itemType;
  125.     Handle                itemHandle;
  126.     Rect                itemBounds;
  127.     
  128.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  129.     if (inHasValue)
  130.     {
  131.         SetDialogItemText(itemHandle, inValue);
  132.     }
  133.     else
  134.     {
  135.         SetDialogItemText(itemHandle, "\p");
  136.     }
  137. }
  138.  
  139.  
  140. /* =============================================================================
  141.  *        Utils_GetStr255Field (external)
  142.  *
  143.  *    Interprets the value of the given editable or static text field as an
  144.  *    string value.  If outHasValue is NULL then the field must not be blank.
  145.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  146.  *    and true if it has a value.  Returns false if there is a formatting problem.
  147.  * ========================================================================== */
  148. Boolean Utils_GetStr255Field(
  149.     DialogPtr            inDialog,
  150.     short                inItem,
  151.     Str255                outValue,
  152.     Boolean*            outHasValue)
  153. {
  154.     short                itemType;
  155.     Handle                itemHandle;
  156.     Rect                itemBounds;
  157.     
  158.     // Grab the string
  159.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  160.     GetDialogItemText(itemHandle, outValue);
  161.     
  162.     if (outHasValue != NULL)
  163.     {
  164.         *outHasValue = outValue[0] > 0;
  165.     }
  166.     
  167.     return true;
  168. }
  169.  
  170.  
  171. /* =============================================================================
  172.  *        Utils_SetUInt32Field (external)
  173.  *
  174.  *    Changes the text of the given dialog item to the given integer value.  If
  175.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  176.  *    of course be editable or static text.
  177.  * ========================================================================== */
  178. void Utils_SetUInt32Field(
  179.     DialogPtr            inDialog,
  180.     short                inItem,
  181.     UInt32                inValue,
  182.     Boolean                inHasValue)
  183. {
  184.     Str255                str;
  185.     short                itemType;
  186.     Handle                itemHandle;
  187.     Rect                itemBounds;
  188.     
  189.     if (inHasValue)
  190.     {
  191.         sprintf((char*) str, "x%lu", inValue);
  192.         str[0] = strlen((char*) str) - 1;
  193.     }
  194.     else
  195.     {
  196.         str[0] = 0;
  197.     }
  198.     
  199.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  200.     SetDialogItemText(itemHandle, str);
  201. }
  202.  
  203.  
  204. /* =============================================================================
  205.  *        Utils_GetUInt32Field (external)
  206.  *
  207.  *    Interprets the value of the given editable or static text field as an
  208.  *    integer value.  If outHasValue is NULL then the field must not be blank.
  209.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  210.  *    and true if it has a value.  Returns false if there is a formatting problem.
  211.  * ========================================================================== */
  212. Boolean Utils_GetUInt32Field(
  213.     DialogPtr            inDialog,
  214.     short                inItem,
  215.     UInt32*                outValue,
  216.     Boolean*            outHasValue,
  217.     UInt32                inMin,
  218.     UInt32                inMax)
  219. {
  220.     #pragma unused (inMin, inMax)
  221.     Str255                str;
  222.     short                itemType;
  223.     Handle                itemHandle;
  224.     Rect                itemBounds;
  225.     
  226.     // Grab the string
  227.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  228.     GetDialogItemText(itemHandle, str);
  229.     str[str[0]+1] = 0;
  230.     
  231.     // Parse it
  232.     if (sscanf((char*) str, "%*c%lu", outValue) > 0)
  233.     {
  234.         // Got a number
  235.         if (outHasValue != NULL)
  236.         {
  237.             *outHasValue = true;
  238.         }
  239.         
  240.         // Check the range
  241.         #if HONOR_RANGE
  242.             if (*outValue < inMin || *outValue > inMax)
  243.             {
  244.                 return false;
  245.             }
  246.         #endif
  247.     }
  248.     else
  249.     {
  250.         if (outHasValue != NULL)
  251.         {
  252.             *outHasValue = false;
  253.         }
  254.         else
  255.         {
  256.             return false;
  257.         }
  258.     }
  259.     
  260.     return true;
  261. }
  262.  
  263.  
  264. /* =============================================================================
  265.  *        Utils_SetFloatField (external)
  266.  *
  267.  *    Changes the text of the given dialog item to the given float value.  If
  268.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  269.  *    of course be editable or static text.
  270.  * ========================================================================== */
  271. void Utils_SetFloatField(
  272.     DialogPtr            inDialog,
  273.     short                inItem,
  274.     float                inValue,
  275.     Boolean                inHasValue)
  276. {
  277.     Str255                str;
  278.     short                itemType;
  279.     Handle                itemHandle;
  280.     Rect                itemBounds;
  281.     float                abs;
  282.     float                fract;
  283.     
  284.     if (inHasValue)
  285.     {
  286.         abs = fabsf(inValue);
  287.         fract = abs-floorf(abs);
  288.         if (fract < 0.0001 || fract > 0.9999)
  289.         {
  290.             // Value is close enough to integer to show it that way
  291.             sprintf((char*) str, "x%.0f", inValue);
  292.         }
  293.         else
  294.         {
  295.             // Has a fractional part
  296.             sprintf((char*) str, "x%.2f", inValue);
  297.         }
  298.         str[0] = strlen((char*) str) - 1;
  299.     }
  300.     else
  301.     {
  302.         str[0] = 0;
  303.     }
  304.     
  305.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  306.     SetDialogItemText(itemHandle, str);
  307. }
  308.  
  309.  
  310. /* =============================================================================
  311.  *        Utils_GetFloatField (external)
  312.  *
  313.  *    Interprets the value of the given editable or static text field as an
  314.  *    float value.  If outHasValue is NULL then the field must not be blank.
  315.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  316.  *    and true if it has a value.  Returns false if there is a formatting problem.
  317.  * ========================================================================== */
  318. Boolean Utils_GetFloatField(
  319.     DialogPtr            inDialog,
  320.     short                inItem,
  321.     float*                outValue,
  322.     Boolean*            outHasValue,
  323.     float                inMin,
  324.     float                inMax)
  325. {
  326.     #pragma unused (inMin, inMax)
  327.     Str255                str;
  328.     short                itemType;
  329.     Handle                itemHandle;
  330.     Rect                itemBounds;
  331.     
  332.     // Grab the string
  333.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  334.     GetDialogItemText(itemHandle, str);
  335.     str[str[0]+1] = 0;
  336.     
  337.     // Parse it
  338.     if (sscanf((char*) str, "%*c%f", outValue) > 0)
  339.     {
  340.         // Got a number
  341.         if (outHasValue != NULL)
  342.         {
  343.             *outHasValue = true;
  344.         }
  345.         
  346.         // Check the range
  347.         #if HONOR_RANGE
  348.             if (*outValue < inMin || *outValue > inMax)
  349.             {
  350.                 return false;
  351.             }
  352.         #endif
  353.     }
  354.     else
  355.     {
  356.         if (outHasValue != NULL)
  357.         {
  358.             *outHasValue = false;
  359.         }
  360.         else
  361.         {
  362.             return false;
  363.         }
  364.     }
  365.     
  366.     return true;
  367. }
  368.  
  369.  
  370. /* =============================================================================
  371.  *        Utils_SetVector3DField (external)
  372.  *
  373.  *    Changes the text of the given dialog item to the given TQ3ector3D value.  If
  374.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  375.  *    of course be editable or static text.
  376.  * ========================================================================== */
  377. void Utils_SetVector3DField(
  378.     DialogPtr            inDialog,
  379.     short                inItem,
  380.     const TQ3Vector3D*    inValue,
  381.     Boolean                inHasValue)
  382. {
  383.     Str255                str;
  384.     short                itemType;
  385.     Handle                itemHandle;
  386.     Rect                itemBounds;
  387.     float                abs;
  388.     float                fract;
  389.     UInt32                xPrecision;
  390.     UInt32                yPrecision;
  391.     UInt32                zPrecision;
  392.     
  393.     if (inHasValue)
  394.     {
  395.         abs = fabsf(inValue->x);
  396.         fract = abs-floorf(abs);
  397.         xPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  398.         
  399.         abs = fabsf(inValue->y);
  400.         fract = abs-floorf(abs);
  401.         yPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  402.         
  403.         abs = fabsf(inValue->z);
  404.         fract = abs-floorf(abs);
  405.         zPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  406.         
  407.         sprintf((char*) str, "x%.*f %.*f %.*f", xPrecision, inValue->x, yPrecision, inValue->y, zPrecision, inValue->z);
  408.         str[0] = strlen((char*) str) - 1;
  409.     }
  410.     else
  411.     {
  412.         str[0] = 0;
  413.     }
  414.     
  415.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  416.     SetDialogItemText(itemHandle, str);
  417. }
  418.  
  419.  
  420. /* =============================================================================
  421.  *        Utils_GetVector3DField (external)
  422.  *
  423.  *    Interprets the value of the given editable or static text field as an
  424.  *    TQ3Vector3D value.  If outHasValue is NULL then the field must not be blank.
  425.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  426.  *    and true if it has a value.  Returns false if there is a formatting problem.
  427.  * ========================================================================== */
  428. Boolean Utils_GetVector3DField(
  429.     DialogPtr            inDialog,
  430.     short                inItem,
  431.     TQ3Vector3D*        outValue,
  432.     Boolean*            outHasValue)
  433. {
  434.     Str255                str;
  435.     short                itemType;
  436.     Handle                itemHandle;
  437.     Rect                itemBounds;
  438.     
  439.     // Grab the string
  440.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  441.     GetDialogItemText(itemHandle, str);
  442.     str[str[0]+1] = 0;
  443.     
  444.     // Parse it
  445.     if (sscanf((char*) str, "%*c%f%f%f", &outValue->x, &outValue->y, &outValue->z) == 3)
  446.     {
  447.         // Got a number
  448.         if (outHasValue != NULL)
  449.         {
  450.             *outHasValue = true;
  451.         }
  452.     }
  453.     else
  454.     {
  455.         if (outHasValue != NULL)
  456.         {
  457.             *outHasValue = false;
  458.         }
  459.         else
  460.         {
  461.             return false;
  462.         }
  463.     }
  464.     
  465.     return true;
  466. }
  467.  
  468.  
  469.  
  470.